package com.thprojects.myphotomemories.view;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;

import com.thprojects.myphotomemories.controller.ContactsController;
import com.thprojects.myphotomemories.controller.FSController;
import com.thprojects.myphotomemories.controller.OnGotContactsListener;
import com.thprojects.myphotomemories.model.Contact;
import com.thprojects.myphotomemories.model.Photo;

import exception.PhotoSourceNotFoundException;
import exception.SDNotReadyException;

public class PhotoDetailsEdit extends PhotoDetailsView{
	
	FSController mFSController;
	Photo mPhoto;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
	}
	
	protected void getIntentInfo(){
		Intent i = getIntent();
        mPhoto = (Photo)i.getParcelableExtra("photo");
	}

	
	protected void setInfoInGui(){
		mNewPhotoFilePath.setText(mPhoto.getPath());
		mNewPhotoName.setText(mPhoto.getName());
        mNewPhotoCaption.setText(mPhoto.getCaption());
        mNewPhotoLocation.setText(mPhoto.getLocation());
        
        mFSController = FSController.getFSController(this);
		

        Display d = getWindowManager().getDefaultDisplay();
		try {
			mImage = mFSController.decodeSampledBitmap(mPhoto.getPath(), d.getWidth(), d.getWidth());
			mNewPhoto.setImageBitmap(mImage);
		} catch (PhotoSourceNotFoundException e) {
		} catch (SDNotReadyException e) {
		}
		
		
		mContacts = mPhoto.getContacts();
		mContactsController = new ContactsController(mContacts, PhotoDetailsEdit.this, new OnGotContactsListener() {
			public void onGotContacts(List<Contact> contacts) {
				mContacts = contacts;
				updateContactListView();
			}
		}); 
		updateContactListView();
	}
	
	protected void setSaveAndCancelListeners(){
		setSaveAndCancelListeners(
        	new OnClickListener() {
				public void onClick(View v) {
					mDbController.editPhoto(mPhoto.getId(), mNewPhotoName.getText().toString(), mNewPhotoCaption.getText().toString(), mNewPhotoLocation.getText().toString(), mContacts);
					mPhoto.setName(mNewPhotoName.getText().toString());
					mPhoto.setCaption(mNewPhotoCaption.getText().toString());
					mPhoto.setLocation(mNewPhotoLocation.getText().toString());
					mPhoto.setContacts(mContacts);
					finish();
				}
        	},
        	new OnClickListener() {		
				public void onClick(View v) {
					finish();
				}
        	}
        );
	}

}
